/**
 * 
 */
package com.moyq5.permit.center.web.controller;

import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.moyq5.permit.center.web.jackson.MixInFactory;

/**
 * Controller 基类
 * 
 * @author Moyq5
 * @date 2016年9月19日
 */
public class BaseController extends AbstractLoginController {
	
	private static final ObjectMapper om;
	
	static {
		Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
		
		builder.featuresToEnable(SerializationFeature.WRITE_ENUMS_USING_INDEX);
		builder.featuresToEnable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);
		
		builder.featuresToDisable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
		builder.featuresToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
		builder.featuresToDisable(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS);
		
		builder.mixIns(new MixInFactory().getMixIns());
		
		om = builder.build();
	}
	
	protected String toJson(Object obj, FilterProvider provider) {
		ObjectWriter ow = om.writer().with(provider);
		try {
			return ow.writeValueAsString(obj);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
